﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Services;
using System.Web.Security;
using cognysClient.CognyService;

namespace cognysClient
{
    public partial class StroopEffect : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
       
        }
        //method for saving the stroop result.
        [WebMethod]
        public static void getStroopResults(int[] results)
        {
            try
            {
                //The id for the logged in user.
                Guid person = Guid.Parse(Membership.GetUser().ProviderUserKey.ToString());

                var client = new Service1Client();
                var baseline = client.getBaseline(person);
                var resultState = client.getResultState(person);

                if (resultState.StateStroop == null)
                {

                    client.editBaseLine(person, "stroop", results[2]);

                    client.editResultState(person, "stroop", 1);

                }

                else if (resultState.StateStroop == 1)
                {
                    if (results[2] > baseline.ScoreStroop)
                    {
                        client.editBaseLine(person, "stroop", results[2]);
                    }
                    client.editResultState(person, "stroop", 2);
                }
                else
                {
                    ResultStroop sr = new ResultStroop();
                    sr.Person = person;
                    sr.regDate = DateTime.Now;
                    sr.compTime = results[0];
                    sr.Error = results[1];
                    sr.Score = results[2];
                    client.AddStroopResult(sr);
                }   
            }
            catch
            {
                
            }
        }
        //method for displaying the persons baseline
        [WebMethod]
        public static int GetBl()
        {
            try
            {
                using (var context = new Service1Client())
                {
                    return (int)context.getBaseline(Guid.Parse(Membership.GetUser().ProviderUserKey.ToString())).ScoreStroop;
                } 
            }
            catch
            {
                return 0;
            }
        }
    }
}